/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.supplier;

import ejb.model.EJBModel;
import ejb.person.SupplierBeanRemote;
import ejb.person.TelecomBeanRemote;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import util.person.TelecomDetails;
import util.person.TelecomtypeDetails;

/**
 *
 * @author Gebruiker
 */
public class TelecomTabelModel extends AbstractTableModel {

    private final String[] columns = {"Type", "Waarde"};
    private ArrayList<TelecomDetails> telecom;
    private HashMap<String, Integer> typesString;
    private HashMap<Integer, String> typesInt;

    public TelecomTabelModel() {
        telecom = new ArrayList<TelecomDetails>();
        typesString = new HashMap<String, Integer>();
        typesInt = new HashMap<Integer, String>();
        for(TelecomtypeDetails type:EJBModel.getTelecomtypeBean().getAllTelecomtypeDetails()){
            typesString.put(type.getName(), type.getId());
            typesInt.put(type.getId(),type.getName());
        }
    }

    @Override
    public int getRowCount() {
        return telecom.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return true;
    }

    @Override
    public Object getValueAt(int row, int column) {
        if (column == 0) {
            return typesInt.get(telecom.get(row).getTelecomTypeid());
        } else {
            return telecom.get(row).getValue();
        }
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        if (column == 0) {
            telecom.get(row).setTelecomTypeid(typesString.get(aValue));
        } else {
            telecom.get(row).setValue(aValue.toString());
        }
    }

    public void addValue() {
        TelecomDetails telecomDetails = new TelecomDetails(-1, "Waarde", 1);
        telecom.add(telecomDetails);
        this.fireTableDataChanged();
    }
    
    public void saveTelecom(Integer id){
        TelecomBeanRemote tbr = EJBModel.getTelecomBean();
        SupplierBeanRemote sbr = EJBModel.getSupplierBean();
        for(TelecomDetails td:telecom){
            if(td.getId()<0){
                Integer tid = tbr.createTelecom(td.getValue(), td.getTelecomTypeid(), id);
                td.setId(tid);
                sbr.addTelecomToSupplier(id, tid);
            }
        }
    }

    public void editTelecom() {
        TelecomBeanRemote tbr = EJBModel.getTelecomBean();
        for(TelecomDetails td:telecom){
            if(td.getId()>=0){
                tbr.editTelecom(td.getValue(), td.getTelecomTypeid(), td.getId());
            }
        }
    }

    public void loadTelecom(Collection<Integer> telecomCollection) {
        TelecomBeanRemote tbr = EJBModel.getTelecomBean();
        telecom = new ArrayList<TelecomDetails>();
        for(Integer index:telecomCollection){
            telecom.add(tbr.getTelecomDetails(index));
        }
        this.fireTableDataChanged();
    }
    
    public void clearTelecom(){
        telecom = new ArrayList<TelecomDetails>();
        this.fireTableDataChanged();
    }

    public void deleteTelecom(Collection<Integer> telecomCollection) {
        TelecomBeanRemote tbr = EJBModel.getTelecomBean();
        for(Integer id:telecomCollection){
            tbr.removeTelecom(id);
        }
    }

    
    
}
